#include "AnaDef.h"
#include "XGLUtils.h"

void Print(Char_t* tn, Char_t* cut, Char_t* var, const Double_t ni, const Double_t ntot)
{
  const TGraphAsymmErrors * gr = XGLUtils::TreeToGraph(tn, cut, (Char_t*) "xx", var);
  //TGraph::GetMean, RMS checked with array
  const Double_t mean = gr->GetMean(2)*ntot;
  const Double_t rms = gr->GetRMS(2)*ntot;
  printf("clac::Print tree %10s cut %10s var %10s N %10.0f Mean %10.3f RMS %10.3f (Mean-N)/sqrt(N) %10.3f Sigma/sqrt(N) %10.3f\n", tn, cut, var, ni, mean, rms, (mean-ni)/sqrt(ni), rms/sqrt(ni));
  delete gr;
}

int main(int argc, char * argv[])
{
  for(int ii=0; ii<argc; ii++){
    printf("%d: %s\n", ii, argv[ii]);
  }

  if(argc!=6){
    printf("argc!=6\n"); return 1;
  }

  TFile::Open(argv[1]);
  Char_t * tn =argv[2];
  Char_t * var = argv[3];
  const Double_t ni=atof(argv[4]);
  const Double_t ntot=atof(argv[5]);

  Print(tn, (Char_t*)"1", var, ni, ntot);
  Print(tn, (Char_t*)"xx>TMath::Log10(4)&&xx<TMath::Log10(20)", var, ni, ntot);
 
  return 0;
}
